var $_plugin852ActionName = {
    controlSwitch: "controlSwitch",
    getDeviceInfo: "getDeviceInfo"
};

//console.log(parse_852('02000E618B5A4705000000000000000000',{}));

function parse_852(data, res) {
    var time = "";
    switch (data.substr(0,2)) {
        case '01':
            var state = data.substr(10, 2);
            var elec = data.substr(6, 2);
            res.base = parseInt(state, 16)  + "";
            res.elec = parseInt(elec, 16)  + "";
            return res
        case '02':
            var state = data.substr(14, 2);
            var userid = data.substr(16, 4);
            time = parseInt(data.substr(6, 8), 16) * 1000;
            res.state = parseInt(state, 16) + "";
            res.openTime = time;
            res.userid = parseInt(userid, 16) + "";
            return res
        case '03':
            var state = data.substr(14, 2);
            var type = data.substr(16, 2);
            var userid = data.substr(18, 4);
            time = parseInt(data.substr(6, 8), 16)
            res.alarm = parseInt(state, 16) + "";
            res.alarmTime = time;
            res.userid = parseInt(userid, 16) + "";
            res.type = parseInt(type, 16) + "";
            return res
        case '16':
        	var data1 = data.substr(6, parseInt(data.substr(2,4),16) * 2)
            var type = data1.substr(0, 2)
            var d = data1.substr(10)
            var userlist = []
        	for (var x = 0; x < parseInt(d.length / 14); x++) {
        		var item = d.substr(x * 14, 14)
                var t = parseInt(item.substr(0, 2), 16)
                userlist.push({
					"id": parseInt(item.substr(2, 4), 16),
					"type": parseInt(item.substr(0, 2), 16),
					"time": parseInt(item.substr(6), 16) * 1000
				})
        	}
            res.type = type;
            res.userlist = userlist
            return res;
    }
}

/**
 *
 * @param {Object} playload 串口净荷数据
 * @param {Object} byteDefineCodeArray 串口协议byte字段定义
 * @param {Object} byteDefineKeyArray 串口协议json解析后的字段定义
 */
function returnParseUartData_852(playload, byteDefineCodeArray, byteDefineKeyArray) {
    if (typeof (playload) == "undefined" || playload == null || playload == "" || byteDefineKeyArray == null ||
        byteDefineKeyArray == null || byteDefineKeyArray.length == 0 || (byteDefineKeyArray.length != byteDefineCodeArray.length)
    ) {
        return null;
    }
    playload = playload.toUpperCase().replace(/ /g, "");
    var playloadUartDataJson = "";
    var subStrStart = 0;
    for (var i = 0; i < byteDefineCodeArray.length; i++) {
        var byteHexStrLength = byteDefineCodeArray[i] * 2;
        var byteKey = byteDefineKeyArray[i];

        var value = playload.substr(subStrStart, byteHexStrLength);

        if (i != (byteDefineCodeArray.length - 1)) {
            playloadUartDataJson += "\"" + byteKey + "\":\"" + parseInt(value, 16) + "\",";
        } else {
            playloadUartDataJson += "\"" + byteKey + "\":\"" + parseInt(value, 16) + "\"";
        }
        subStrStart += byteHexStrLength;
    }
    return JSON.parse("{" + playloadUartDataJson + "}");
}

function decode_852(buzJson) {
    buzJson = buzJson.replace(/\s+/g, "").toUpperCase();
    var uartDataArray = buzJson.split("AA");
    for (var i = uartDataArray.length; i > 0; i--) {
        var uartdata = ("AA" + uartDataArray[i - 1]).replace(/ /g, "");
        if (uartdata.indexOf("AA") >= 0) {
            uartdata = uartdata.replace("AA", "");
        } else {
            break;
        }
        if (uartdata.length < 8) {
            break;
        }
        var code = uartdata.substr(4, 2);
        var len = parseInt(uartdata.substr(0, 4), 16);
        if ((len * 2 + 6) != uartdata.length) {
            break;
        }
        var payload = uartdata.substring(6, uartdata.length - 2);
        var data = payload;
        var x = 0;
        var d
        var res = {};
        for (var x = 1; x <= 10; x++) {
            data = payload.substring(0, parseInt(payload.substr(2, 4), 16) * 2 + 6)
            len = parseInt(payload.substr(2, 4), 16);
            code = payload.substr(0, 2)
            x++;
            d = {
                "code": code,
                "len": len,
                "payload": data
            }
            parse_852(d, res);
            payload = payload.replace(data, "")
        }

        return {
            "resultCode": "1",
            "actionName": "getDeviceInfo",
            "actionParams": res
        };

    }
    return null;
}

function getCheckCode_852(fullData) {
    fullData = fullData.toUpperCase().replace(/ /g, "");
    var intData = 0;
    for (var i = 0; i < fullData.length; i++) {
        var _d = fullData.substr(i, 2);
        intData += parseInt(_d, 16);
        i++;
    }
    var hexCheckCode = intData.toString(16);
    if (hexCheckCode.length == 1) {
        hexCheckCode = "0" + hexCheckCode;
    }
    hexCheckCode = hexCheckCode + "";
    return hexCheckCode.substring(hexCheckCode.length - 2, hexCheckCode.length).toUpperCase();
}